import typing

from PySide6.QtCore import Qt
from PySide6.QtGui import QIcon
from PySide6.QtWidgets import QTreeWidgetItem


class TreeWidgetItemABC(QTreeWidgetItem):
    '''
    description:
        在 Trace 窗体内部展示的 TreeWidgetItem 基类，具有以下功能
        - 右键弹出菜单栏，菜单栏中有常用功能
        - SetDisplayConfig
        - ........
    args: 
        - flag 'str' 使用 d_data 中的哪个 key 作为本 Item 的标志，用于实现 TreeWidget 原地更新
        - d_data: 用于刷新数据
    可能需要重写的函数:
        
    '''
    def __init__(self, flag: 'str', d_data: 'dict' = dict(), icon_table_: 'dict' = dict()) -> None:
        ################################
        super().__init__()
        self.itemDict = d_data     # 当本对象插入到 TreeWidget 中
        self.flag = flag           # 本 item 对象的 Flag
        self._iconTable = icon_table_   # 用来添加 icon 的表
        self.setFlags(Qt.NoItemFlags)

    def Refresh(
        self, 
        parent_header_config: typing.Dict['str', 'dict'], 
        child_header_config: typing.Dict['str', 'dict']) -> None:
        '''
        function:
            根据当前的header配置刷新TreeWidgetItem的数据
        '''
        for header_name_, config_ in parent_header_config.items():
            text_ = self.itemDict.get(header_name_, None)
            if (text_ == None):
                continue
            # index_ = config_.get('index', None)
            # if (not isinstance(index_, int)):
            #     continue
            if (isinstance(text_, bytearray)):
                self.setText(config_['index'], text_.hex())
            else:
                self.setText(config_['index'], str(text_))
            # 设置 Icon ========================
            self.setIcon(0, self._iconTable.get(self.itemDict.get('eventType', ''), QIcon()))
            # ==================================
        # 清除当前 item 中的子item ===============
        # for child_index_ in range(self.childCount()):
        #     child_item_ = self.child(child_index_)
        #     self.removeChild(child_item_)
        # =======================================
        # children_ = self.itemDict.get('children', list())
        # if (not isinstance(children_, list)):
        #     return
        # child_l_ = list()
        # for child_ in children_:
        #     if (not isinstance(child_, dict)):
        #         continue
        #     item_child_ = TreeWidgetItemABC('', child_, self._iconTable)
        #     for header_name_, config_ in child_header_config.items():
        #         text_ = item_child_.itemDict.get(header_name_, '')
        #         # if (text_ == None):
        #         #     continue
        #         index_ = config_.get('index', None)
        #         # if (not isinstance(index_, int)):
        #         #     continue
        #         if (isinstance(text_, bytearray)):
        #             item_child_.setText(config_['index'], text_.hex())
        #         else:
        #             item_child_.setText(config_['index'], str(text_))
        #         # 添加 Icon ========================
        #         # item_child_.setIcon(0, item_child_._iconTable.get(item_child_.itemDict.get('eventType', ''), QIcon()))
        #         # ==================================
        #     child_l_.append(item_child_)
        # self.addChildren(child_l_)
